package cn.com.yinhex.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;


import cn.com.yinhex.dao.UserDao;
import cn.com.yinhex.domain.User;
import cn.com.yinhex.util.Pager;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
	private static final Logger logger = Logger.getLogger(UserDaoImpl.class);
	@Resource(name = "sqlMapClientTemplate")
    private SqlMapClientTemplate sqlMapClientTemplate;
	public User login(String email, String password) throws Exception {
		Map map = new TreeMap();
		map.put("email", email);
		map.put("password", password);
		User user = (User) sqlMapClientTemplate.queryForObject("user.login",map);
		System.out.println("11111111111111111我成功了");
		return user;
		
		
	}
	public void insert(User user) throws Exception {
		sqlMapClientTemplate.insert("user.insert", user);
		
	}
	public List<User> getUserList(String keyword,Pager pager) throws Exception {
		Map<String, Object> props = new HashMap<String, Object>();
		if (pager != null) {
			int total = (Integer) sqlMapClientTemplate.queryForObject("user.selectListCount",keyword);
			if (total > 0) {
				pager.setTotal(total);
				pager.limit();
				props.put("sortname", pager.getSortname());
				props.put("sortorder", pager.getSortorder());
				props.put("firstResult", pager.getPageFirstIndex());
				props.put("maxResult", pager.getPageSize());
                props.put("keyword", keyword);
			} else {
				return new ArrayList<User>(0);
			}
		}
       
		return (List<User>) sqlMapClientTemplate.queryForList("user.selectList", props);
	}
	public void delete(String userid) throws Exception {
		sqlMapClientTemplate.delete("user.delete", userid);
		
		
	}
	public void updateUser(User user) throws Exception {
		sqlMapClientTemplate.update("user.update", user);
		
	}
	public User getUserById(String userId) throws Exception {
		User user = (User) sqlMapClientTemplate.queryForObject("user.selectUserById",userId);
		return user;
	}
	
	
	

}
